//
//  RoadViewController.m
//  EagleEye
//
//  Created by 18_李明明 on 15-1-21.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "RoadViewController.h"

@interface RoadViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *roadContent;
//创建一个装有路线详情的数组
@property (nonatomic, strong) NSArray *contentArr;
@property (nonatomic, strong) UILabel *start;
@property (nonatomic, strong) UILabel *stop;
@property (nonatomic, strong) UIButton *firstDirection;
@property (nonatomic, strong) UIButton *secondDirection;
@end

@implementation RoadViewController
- (instancetype)init
{
    self = [super init];
    if (self) {
        self.contentArr = [NSArray array];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NSLog(@"%@", [self.roadArr firstObject]);
    // 正方向button
    self.view.backgroundColor = [UIColor whiteColor];
    self.firstDirection = [UIButton buttonWithType:UIButtonTypeSystem];
    self.firstDirection.frame = CGRectMake(0, 20, WIDTH / 2, 40);
    [self.firstDirection setTitle:@"正方向" forState:UIControlStateNormal];
    [self.view addSubview:self.firstDirection];
    self.firstDirection.backgroundColor = [UIColor lightGrayColor];
    [self.firstDirection addTarget:self action:@selector(targetFirst:) forControlEvents:UIControlEventTouchUpInside];
    // 反方向
    self.secondDirection = [UIButton buttonWithType:UIButtonTypeSystem];
    self.secondDirection.frame = CGRectMake(WIDTH / 2, 20, WIDTH / 2, 40);
    [self.secondDirection setTitle:@"反方向" forState:UIControlStateNormal];
    [self.view addSubview:self.secondDirection];
    [self.secondDirection addTarget:self action:@selector(targetSecond:) forControlEvents:UIControlEventTouchUpInside];
    // 创建tableview
    self.roadContent = [[UITableView alloc] initWithFrame:CGRectMake(0, 60, WIDTH, HEIGHT - 104) style:UITableViewStyleGrouped];
    self.roadContent.delegate = self;
    self.roadContent.dataSource = self;
    [self.view addSubview:self.roadContent];
    [self handleStr:[self.roadArr firstObject]];
    // 添加一个返回的按钮
    UIButton *cancle = [UIButton buttonWithType:UIButtonTypeSystem];
    cancle.frame = CGRectMake(0, HEIGHT - 40, 40, 30);
//    [cancle setTitle:@"返回" forState:UIControlStateNormal];
    [cancle setBackgroundImage:[UIImage imageNamed:@"cancle.png"] forState:UIControlStateNormal];
    [self.view addSubview:cancle];
    [cancle addTarget:self action:@selector(targetCancle:) forControlEvents:UIControlEventTouchUpInside];
    // 创建两个label
    self.start = [[UILabel alloc] initWithFrame:CGRectMake(0, 60, WIDTH / 2, 40)];
    self.start.backgroundColor = [UIColor whiteColor];
    self.start.textAlignment = NSTextAlignmentCenter;
    
    [self.view addSubview:self.start];
    self.stop = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH / 2, 60, WIDTH / 2, 40)];
    self.stop.backgroundColor = [UIColor whiteColor];
    self.stop.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:self.stop];
    [self targetFirst:nil];
}
// 返回按钮方法
- (void)targetCancle:(UIButton *)cancle
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.contentArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"0502"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"0502"];
    }
    cell.textLabel.text = [self.contentArr objectAtIndex:indexPath.row];
    return cell;
}
// 正方向按钮
- (void)targetFirst:(UIButton *)first
{
    self.firstDirection.backgroundColor = [UIColor lightGrayColor];
    self.secondDirection.backgroundColor = [UIColor whiteColor];
    [self handleStr:[self.roadArr firstObject]];
}
// 反方向按钮
- (void)targetSecond:(UIButton *)second
{
    second.backgroundColor = [UIColor lightGrayColor];
    self.firstDirection.backgroundColor = [UIColor whiteColor];
    [self handleStr:[self.roadArr lastObject]];
}
// 对字符串进行处理
- (void)handleStr:(NSString *)str
{
    NSArray *tempArr = [str componentsSeparatedByString:@"\n"];
    NSString *tempStr = [tempArr lastObject];
    self.contentArr = [tempStr componentsSeparatedByString:@";"];
    self.start.text = [self.contentArr firstObject];
    self.stop.text = [self.contentArr lastObject];
    [self.roadContent reloadData];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
